import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

file = 'test.json'
# 读取文件并将地址写入列表
def read_url_write_list(file):
    list = []
    with open(file, encoding='gbk', errors='ignore') as load_f:
        load_dict = json.load(load_f)
        for i in load_dict['list']:
            for j in i['list']:
                # print(j)
                try:
                    if j['type'] == 3:
                        list.append(j['material'])
                except:
                    for k in j['list']:
                        # print(k)
                        if k['type'] ==3:
                            list.append(k['material'])
    return list

list = ['http://ydschool-online.nos.netease.com/200903_154625-E01%C2%B7%E8%8B%B1%E8%AF%AD%E7%9A%84%E5%8F%A5%E5%AD%90%E6%88%90%E5%88%86%E5%92%8C%E4%BA%94%E5%A4%A7%E5%9F%BA%E6%9C%AC%E5%8F%A5%E5%9E%8B.pdf']
# 通过selenium下载pdf类（不好用）
class PdfDownload:
    # 打开url
    def open(self, url):
        self.driver = webdriver.Chrome()
        self.driver.get(url)
    # 关闭url
    def close(self):
        self.driver.close()
    # 读取pdf链接并且下载文件
    def pdf_download(self, list):
        for url in list:
            self.open(url)
            demo = self.driver.find_element_by_tag_name('body')
            demo.send_keys(Keys.CONTROL, 's')
            sleep(5)
            self.close()

# request 下载内容
def download_video(url_list, save_folder):
    import requests
    from tqdm import tqdm
    from urllib.parse import unquote
    for url in tqdm(url_list):
        url = unquote(url)
        file = save_folder + url.split('/')[-1]  # 保存本地的路径
        r = requests.get(url)  #根据文件的大小，这一步为主要耗时步骤
        with open(file, "wb") as code:
            code.write(r.content)
            sleep(3)
    print('download over')

if __name__ == "__main__":
    url_list = read_url_write_list(file)
    save_folder = 'data/'
    download_video(url_list, save_folder)
